package com.ly.mpayment.rest.system;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.BlackList;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.bill.BillOrderService;
import com.ly.mpayment.persist.service.blacklist.IBlackListService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.persist.task.BillTask;
import com.ly.mpayment.rest.AbsRest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.util.Map;

@Component
@Path("system/billOrder/")
public class BillOrderRest extends AbsRest{
    @Autowired
    BillOrderService billOrderService;

    @Autowired
    IUserService userService;

    @Autowired
    BillTask billTask;

    @Path("queryBillDown")
    @POST
    @SuppressWarnings("static-access")
    public String queryBillDown(String data,@Context HttpServletRequest request){
        String result = this.validationParameter(data, request, userService,"start","pageSize","useruuid");
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {

                        User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                        //如果是下游客户 只能导出一个月的
                        Map<String, Object> parameters = MapUtil.getMap();
                        //如果是下游客户 只能导出一个月的
                        if (login != null && UserUtil.checkRoleDown(login)) {
                            parameters.put(":useruuid",login.getUseruuid());
                        }else{
                            if(!StringUtils.isBlank(json.getString("useruuid"))){
                                parameters.put(":useruuid", json.getString("useruuid"));
                            }
                        }
                        parameters.put(":type",1);
                        if(!StringUtils.isBlank(json.getString("starttime"))){
                            parameters.put(":starttime", DateUtils.parseDate(json.getString("starttime")+" 00:00:00"));
                        }

                        if(!StringUtils.isBlank(json.getString("endtime"))){
                            parameters.put(":endtime", DateUtils.parseDate(json.getString("endtime") +" 23:59:59"));
                        }
                        if(!StringUtils.isBlank(json.getString("fileType"))){
                            parameters.put(":fileType",json.getString("fileType") );
                        }

                            return ResultMessage.getSucceed( JsonUtils.excludes(this.billOrderService.findPageBillOrder(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("queryBillUp")
    @POST
    @SuppressWarnings("static-access")
    public String queryBillUp(String data,@Context HttpServletRequest request){
        String result = this.validationParameterGetRole(request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {

                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            //如果是下游客户 只能导出一个月的
                            if (login != null && UserUtil.checkRole(login)) {
                                Map<String, Object> parameters = MapUtil.getMap();
                                if(!StringUtils.isBlank(json.getString("useruuid"))){
                                    parameters.put(":useruuid", json.getString("useruuid"));
                                }
                                parameters.put(":type", 2);
                                if(!StringUtils.isBlank(json.getString("starttime"))){
                                    parameters.put(":starttime", DateUtils.parseDate(json.getString("starttime")+" 00:00:00"));
                                }

                                if(!StringUtils.isBlank(json.getString("endtime"))){
                                    parameters.put(":endtime", DateUtils.parseDate(json.getString("endtime") +" 23:59:59"));
                                }

                                if(!StringUtils.isBlank(json.getString("fileType"))){
                                    parameters.put(":fileType",json.getString("fileType") );
                                }
                                return ResultMessage.getSucceed( JsonUtils.excludes(this.billOrderService.findPageBillOrder(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
                            }
                        }
                    }
                }catch (Exception e){
                }
            }
        }
        return result;
    }


    @Path("generateBillUp")
    @POST
    @SuppressWarnings("static-access")
    public String generateBillUp(String data,@Context HttpServletRequest request){
        String result = this.validationParameterGetRole(request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);

            if(StringUtils.isBlank(json.getString("starttime")) || StringUtils.isBlank(json.getString("endtime"))){
                return ResultMessage.getFailt("日期不能为空");
            }
            if(!DateUtils.parseYearDay(json.getString("starttime") +" 00:00:00").equals(
                    DateUtils.parseYearDay(json.getString("endtime")+ " 23:59:59"))){
                return ResultMessage.getFailt("日期不能跨月");
            }

            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            //如果是下游客户 只能导出一个月的
                            if (login != null && UserUtil.checkRole(login)) {
                                billTask.queryUp(json.getString("starttime"),json.getString("endtime"),json.getString("useruuid"));
                                return ResultMessage.getSucceed();
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    @Path("generateBillDown")
    @POST
    @SuppressWarnings("static-access")
    public String generateBillDown(String data,@Context HttpServletRequest request){
        String result = this.validationParameter(data, request, userService,"useruuid");
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            if(StringUtils.isBlank(json.getString("starttime")) || StringUtils.isBlank(json.getString("endtime"))){
                return ResultMessage.getFailt("日期不能为空");
            }
            if(!DateUtils.parseYearDay(json.getString("starttime") +" 00:00:00").equals(
                    DateUtils.parseYearDay(json.getString("endtime")+ " 23:59:59"))){
                return ResultMessage.getFailt("日期不能跨月");
            }
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {

                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            //如果是下游客户 只能导出一个月的
                            if (login != null && UserUtil.checkRole(login)) {
                                billTask.queryDown(json.getString("starttime"),json.getString("endtime"),json.getString("useruuid"));
                                return ResultMessage.getSucceed();
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
